﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm xử lý lúc nhận được message từ client trong lượt Main Phase 1
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;
using ManagementEffect;
using ServerYuGi.PythonRegister;

namespace ServerYuGi
{
    public partial class SSupervisor
    {
        void Process_CARD_LENBAI(params object[] param)
        {
            int ID = TurnManager.IDPlayerTrongLuot;

            SPlayer player = LayPlayer(ID);

            SCard card = null;
            foreach (SCard item in player.SoBaiTrenTay)
            {
                if (item.MyYBaseCard.ID == (int)param[0])
                {
                    card = item;
                    break;
                }
            }

            YMSG TrangThai = (YMSG)param[1];
            card.TrangThai = TrangThai;
            

            if (card.MyYBaseCard.CardType == YType.Monster)
            {                
                if (Players[ID].SoSummonTrenBan == Game.SoBaiToiDaTrenBan)
                {                  
                    SendMessage_CARD_LENBAI_KHONGHOPLE_ToID(ID);                    
                    SendMessage_CARD_LENBAI_ToID(ID);
                }
                else
                {  
                    int vitriLaBaiTrenTay = player.SoBaiTrenTay.IndexOf(card);
                    player.LenBai(card);     
                   
                    player.Block.DuocPhep_TanCong.Add(card.MyYBaseCard.ID);
                    SendMessage_CARD_LENBAI_HOPLE_ToID(ID, card.MyYBaseCard.ID, card.TrangThai);
                    SendMessage_CARD_XOABAI_DOITHU_TAY_ToID(LayPlayerKhacVoiPlayerDangTrongLuot().ID,vitriLaBaiTrenTay);
                }
            }
         
        }
        void Process_CARD_LENBAI_XONG(params object[] param)
        {            
            //Block Khong Cho Ra Bai Tiep 
            int ID = TurnManager.IDPlayerTrongLuot;
            SPlayer player = LayPlayer(ID);
            player.Block.DuocPhep_RaBaiNormal = false;
            player.Block.DuocPhep_RaBaiSpecial = false;
            // Send Cho Player Kia The Hien
            SPlayer Otherplayer = LayPlayerKhacVoiPlayerDangTrongLuot();            
            SendMessage_CARD_LENBAI_XONG_ToID(Otherplayer.ID, param);

            ExEffectManager.Init();
            ExEffectManager.IDPlayerCheck = TurnManager.IDPlayerTrongLuot;
            ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_SUMMON;
            //int IDCardLenBai = (int)param[1]; //old
            int IDCardLenBai = (int)param[4]; //new
            SCard card = player.LayCardTrenBan(IDCardLenBai);

            CardScript XMLCardScript = CScriptDB.GetScript(card.MyYBaseCard.Card_TypeID);
            if (XMLCardScript != null)
            {
                if (XMLCardScript.IsCheckFirst == YBool.True)
                    DangKiSript(card);
                else
                    pyregister.AddStore(XMLCardScript.GetListCheckPoint(), card.MyYBaseCard.ID);
            }
            RevMSGmanager.isPlayerKoTrongLuot = true;
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_SUMMON };
            Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,id_summon,soluongte", api_manager, IDCardLenBai, new List<int>());
            RevMSGmanager.Init();

        }
        void Process_CARD_LENBAI_SUMMONDACBIET(params object[] param)
        {
            SPlayer player = LayPlayer(TurnManager.IDPlayerTrongLuot);
            SCard SCardSummon = player.LayCardTrenTay((int)param[0]);
            int SoLuongCardThe = (int)param[1];
            YMonsterCard cardSummon = SCardSummon.MyYBaseCard as YMonsterCard;
            int ByteSendOther = 5 + SoLuongCardThe;

            switch (SoLuongCardThe)
            {
                case 1:
                    if (cardSummon.Level < Game.ListStarThe1Summon.First() || cardSummon.Level > Game.ListStarThe1Summon.Last())
                    {
                        Console.WriteLine("La Bai Nay Khong Phai Summon The 1 la");                        
                        SendMessage_CARD_LENBAI_KHONGHOPLE_ToID(player.ID);
                        return;
                    }                        
                    break;
                case 2:
                    if (cardSummon.Level < Game.ListStarThe2Summon.First())
                    {
                        Console.WriteLine("La Bai Nay Khong Phai Summon The 2 la");                        
                        SendMessage_CARD_LENBAI_KHONGHOPLE_ToID(player.ID);
                        return;
                    }    
                    break;

            }
            List<SCard> ListCardThe = new List<SCard>();
            for (int i = 0; i < SoLuongCardThe; i++)
            {
                SCard tmp = player.LayCardTrenBan((int)param[2 + i]);
                if (tmp == null)
                {                    
                    SendMessage_CARD_LENBAI_KHONGHOPLE_ToID(player.ID);                    
                    SendMessage_CARD_LENBAI_ToID(player.ID);
                    Console.WriteLine("La bai khong ton tai tren ban");
                    return;
                }
                ListCardThe.Add(tmp);
            }
            Console.WriteLine("Da qua tat ca cac dieu kien kiem tra");            
            SendMessage_CARD_LENBAI_SUMMONDACBIET_HOPLE_ToID(player.ID, param);
            List<byte> paramsByte = new List<byte>();
            paramsByte.AddRange(BitConverter.GetBytes(SCardSummon.MyYBaseCard.Card_TypeID));
            //object[] paramOther = new object[ByteSendOther + 1];
            //paramOther[0] = SCardSummon.MyYBaseCard.Card_TypeID;
            for (int i = 0; i < ByteSendOther ; i++)
            {
                //paramOther[i + 1] = param[i];
                paramsByte.Add(ServerYuGi.Converter.YConverter.ConvertObjectToByte(param[i]));
            }
            foreach (SCard item in ListCardThe)
            {
                for (int i = 0; i < player.SoBaiTrenBan.Count; i++)
                {
                    if (item == player.SoBaiTrenBan[i])
                    {
                        player.CheckXoaScript(this, item);
                        player.XoaLaBaiHySinh(item);
                        break;
                    }
                    
                }
            }           
            player.LenBai(SCardSummon);            
            player.Block.DuocPhep_TanCong.Add(SCardSummon.MyYBaseCard.ID);
            SPlayer PlayerOther = LayPlayerKhacVoiPlayerDangTrongLuot();            
            //SendMessage_CARD_LENBAI_SUMMONDACBIET_XONG_ToID(PlayerOther.ID, paramOther);
            SendMessage_CARD_LENBAI_SUMMONDACBIET_XONG_ToID(PlayerOther.ID, paramsByte.ToArray());
            Console.WriteLine("Len Bai Summon Dat Biet Xong");

            List<int> ListIDThe = new List<int>();
            foreach (SCard item in ListCardThe)
            {
                ListIDThe.Add(item.MyYBaseCard.ID);
            }

           
            ExEffectManager.IDPlayerCheck = TurnManager.IDPlayerTrongLuot;
            ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_SUMMON;
            ExEffectManager.Init();
            int IDCardLenBai = (int)param[1];
            RevMSGmanager.isPlayerKoTrongLuot = true;
            CardScript XMLCardScript = CScriptDB.GetScript(SCardSummon.MyYBaseCard.Card_TypeID);
            if (XMLCardScript != null)
            {
                if (XMLCardScript.IsCheckFirst == YBool.True)
                    DangKiSript(SCardSummon);
                else
                    pyregister.AddStore(XMLCardScript.GetListCheckPoint(), SCardSummon.MyYBaseCard.ID);
            }
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_SUMMON };
            Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,id_lenbai,soluongte", api_manager, IDCardLenBai, ListIDThe);
            RevMSGmanager.Init();
        }

        
        
        void Process_CARD_LENBAI_SPELL(params object[] param)
        {
            int ID = TurnManager.IDPlayerTrongLuot;

            SPlayer player = LayPlayer(ID);

            SCard card = null;
            foreach (SCard item in player.SoBaiTrenTay)
            {
                if (item.MyYBaseCard.ID == (int)param[0])
                {
                    card = item;
                    break;
                }
            }

            YMSG TrangThai = (YMSG)param[1];
            card.TrangThai = TrangThai;
            
                if (Players[ID].SoTrapHoacSpellTrenBan == Game.SoBaiToiDaTrenBan)
                {
                    SendMessage_CARD_LENBAI_SPELL_KHONGHOPLE_ToID(ID);                    
                   
                }
                else
                {
                    int vitriLaBaiTrenTay = player.SoBaiTrenTay.IndexOf(card);
                    player.LenBai(card);
                   
                    SendMessage_CARD_LENBAI_SPELL_HOPLE_ToID(ID, card.MyYBaseCard.ID, card.TrangThai);
                    SendMessage_CARD_XOABAI_DOITHU_TAY_ToID(LayPlayerKhacVoiPlayerDangTrongLuot().ID, vitriLaBaiTrenTay);
                }
            
        }

        void Process_CARD_LENBAI_SPELL_XONG(params object[] param)
        {          
            SPlayer player = LayPlayerKhacVoiPlayerDangTrongLuot();
            SendMessage_CARD_LENBAI_SPELL_XONG_ToID(player.ID, param);
            player = LayPlayer(TurnManager.IDPlayerTrongLuot);

            //int IDCardLenBai = (int)param[1]; // old
            int IDCardLenBai = (int)param[4];  // new
            SCard card = player.LayCardTrenBan(IDCardLenBai);
            ExEffectManager.IDPlayerCheck = TurnManager.IDPlayerTrongLuot;
            ExEffectManager.CheckPointNow = YCheckPoint.SPELL_SET;
            ExEffectManager.Init();            
            RevMSGmanager.isPlayerKoTrongLuot = true;
            DangKiSript(card);
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.SPELL_SET };
            Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,id_lenbai", api_manager, IDCardLenBai);
            RevMSGmanager.Init();
        }


        void Process_CARD_LENBAI_TRAP(params object[] param)
        {
            int ID = TurnManager.IDPlayerTrongLuot;

            SPlayer player = LayPlayer(ID);

            SCard card = null;
            foreach (SCard item in player.SoBaiTrenTay)
            {
                if (item.MyYBaseCard.ID == (int)param[0])
                {
                    card = item;
                    break;
                }
            }

            YMSG TrangThai = (YMSG)param[1];
            card.TrangThai = TrangThai;

            if (Players[ID].SoTrapHoacSpellTrenBan == Game.SoBaiToiDaTrenBan)
            {
                SendMessage_CARD_LENBAI_TRAP_KHONGHOPLE_ToID(ID);
                //SendMessage_CARD_LENBAI_ToID(ID);
            }
            else
            {
                int vitriLaBaiTrenTay = player.SoBaiTrenTay.IndexOf(card);
                player.LenBai(card);
    
                SendMessage_CARD_LENBAI_TRAP_HOPLE_ToID(ID, card.MyYBaseCard.ID, card.TrangThai);
                SendMessage_CARD_XOABAI_DOITHU_TAY_ToID(LayPlayerKhacVoiPlayerDangTrongLuot().ID, vitriLaBaiTrenTay);
            }

        }

        void Process_CARD_LENBAI_TRAP_XONG(params object[] param)
        {
            SPlayer player = LayPlayerKhacVoiPlayerDangTrongLuot();
            SendMessage_CARD_LENBAI_TRAP_XONG_ToID(player.ID, param);
            player = LayPlayer(TurnManager.IDPlayerTrongLuot);

            //int IDCardLenBai = (int)param[1]; // old
            int IDCardLenBai = (int)param[4]; 
            SCard card = player.LayCardTrenBan(IDCardLenBai);
            ExEffectManager.IDPlayerCheck = TurnManager.IDPlayerTrongLuot;
            ExEffectManager.CheckPointNow = YCheckPoint.TRAP_SET;
            ExEffectManager.Init();
            RevMSGmanager.isPlayerKoTrongLuot = true;
            DangKiSript(card);
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.TRAP_SET };
            Check_EffectExcute(checkpoint, ExEffectManager.IDPlayerCheck, "api_manager,id_lenbai", api_manager, IDCardLenBai);
            RevMSGmanager.Init();
        }
     
    }


}
